package soa.security.web.action;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import soa.security.Constants;
import soa.security.domain.Department;
import soa.security.domain.Group;
import soa.security.domain.LdapTree;
import soa.security.domain.Organization;
import soa.security.domain.Person;
import soa.security.web.export.LdapTreeJsonFormatExport;
import soa.security.web.support.action.LdapModelAction;
import v1.web.WebUtils;

/**
 * 
 * @author hueb
 *
 *         2015年4月22日
 */
public class GroupAction extends LdapModelAction<Group> {

	private static final long serialVersionUID = -5472245576401751336L;

	private String groupId;
	private String subGroupDns;

	public String grid() {
		if (StringUtils.isBlank(organizationId))
			return SUCCESS;

		if (StringUtils.isNotBlank(departmentId)) {
			List<Group> all = groupMgrService.allGroups(organizationId,
					buildContext().getParameters(), subTree);
			updateResultContent(groupMgrService.wrapGroups(groupMgrService
					.allGroups(all, departmentMgrService.allGroups(
							organizationId, departmentId))));
		} else
			updateResultContent(groupMgrService.wrapGroups(groupMgrService
					.allGroups(organizationId, buildContext().getParameters(),
							subTree)));

		return SUCCESS;
	}

	public String edit() {
		Group group = getModel();
		if (StringUtils.isBlank(group.getBid())) {
			group.setBid("GR" + groupMgrService.findMaxNumber(Group.class));
		}

		Organization or = group.getOrganization();
		if (or == null)
			or = getOrganization(organizationId);
		group.setOrganization(or);

		updateResultContent(group);
		orId = group.getOrganization().getBid();
		return SUCCESS;
	}

	public String view() {
		Group group = getModel();
		Organization or = group.getOrganization();
		if (or == null)
			or = getOrganization(organizationId);
		group.setOrganization(or);

		orId = group.getOrganization().getBid();
		return SUCCESS;
	}

	public String doneSave() {
		groupMgrService.addGroup4Mgr(organizationId, getModel(), departmentDns,
				personDns, subGroupDns);
		return SUCCESS;
	}

	public String doneUpdate() {
		groupMgrService.updateGroup4Mgr(organizationId, getModel(),
				departmentDns, personDns, subGroupDns);
		return SUCCESS;
	}

	/**
	 * 指定机构下的部门树，需要checked
	 * 
	 * @return String
	 * 
	 * @author Cre.Gu at 2015年5月29日
	 */
	public String deptInOneOrgCheckTree() {
		List<Department> checked = groupMgrService.findDepartments(
				organizationId, Constants.GROUPS, dnp);
		List<LdapTree> trees = treeMgrService.deptInOneOrgCheckTree(
				organizationId, checked, departmentId);

		LdapTreeJsonFormatExport content = new LdapTreeJsonFormatExport(trees);
		return WebUtils.writeJson(ServletActionContext.getResponse(),
				content.toString());
	}

	/**
	 * 指定机构下的人员树，需要checked
	 * 
	 * @return String
	 * 
	 * @author Cre.Gu at 2015年5月29日
	 */
	public String personInOneOrgCheckTree() {
		List<Person> checked = groupMgrService.allPersons(organizationId,
				getModel().getBid());
		List<LdapTree> trees = treeMgrService.personInOneOrgCheckTree(
				organizationId, checked);

		LdapTreeJsonFormatExport content = new LdapTreeJsonFormatExport(trees);
		return WebUtils.writeJson(ServletActionContext.getResponse(),
				content.toString());
	}

	/**
	 * 指定机构下的用户组树，需要checked，过滤自己
	 * 
	 * @return String
	 * 
	 * @author Cre.Gu at 2015年5月29日
	 */
	public String groupInOneOrgCheckTree() {
		List<Group> checked = groupMgrService.allSubGroups(organizationId,
				getModel().getBid());
		List<LdapTree> trees = treeMgrService.subGroupInOneOrgCheckTree(
				organizationId, dnp, checked);

		LdapTreeJsonFormatExport content = new LdapTreeJsonFormatExport(trees);
		return WebUtils.writeJson(ServletActionContext.getResponse(),
				content.toString());
	}

	@Override
	public String remove() {
		groupMgrService.remove(organizationId, dnp);
		return SUCCESS;
	}

	@Override
	protected Group findByDn(String dn) {
		return groupMgrService.findByDn(dn);
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getSubGroupDns() {
		return subGroupDns;
	}

	public void setSubGroupDns(String subGroupDns) {
		this.subGroupDns = subGroupDns;
	}
}
